package dbms.ypr;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//实现一个sql解析器
public class SqlAnalysis {

    //private static String[] str;
    //private static final String path = "D:\application software\DBMSS";
    //sql语句类型
    private static final String create = "create";
    private static final String help = "help";
    private static final String show = "show";
    private static final String use = "use";
    private static final String quit = "quit";
    private static final String describe = "describe";
    private static final String insert = "insert";
    private static final String select = "select";
    private static final String drop = "drop";
    //grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"
    private static final String grant = "grant";


    //完成语句匹配
    public static void analysis(String sql){
        //str = sql.split(" ");
        String start = "";

        String regex = "^[a-z]+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(sql);

        while(matcher.find()){
            start = matcher.group();
        }
        sql = sql.replaceAll("`","");

        switch (start){
            case create:
                Create.createSql(sql);
                break;
            case help:
                Help.help();
                break;
            case show:
                Show.showSql(sql);
                break;
            case use:
                dbms.ypr.Use.useSql(sql);
                break;
            case quit:
                try {
                    dbms.ypr.SQLConstant.currentDatabase = null;
                    dbms.ypr.SQLConstant.currentUsername = null;
                    dbms.ypr.Start.main(new String[]{});
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case describe:
                Describe.describeSql(sql);
                break;
            case insert:
                Insert.insertSql(sql);
                break;
            case select:
                Select.selectSql(sql);
                break;
            case drop:
                Drop.dropSql(sql);
                break;
            default:
                System.out.println("输入的命令无法识别,可以输入help查看目前支持的sql语句");
                Input.get();
                break;
        }
        //System.out.println(start);
    }


}
